from gettext import find
import os
import json
import requests


# json.loads(content)
# {
#     "name": "0bsd.txt",
#     "path": "_licenses/0bsd.txt",
#     "sha": "be73691957124783ddcf0353a0db98860dd3de62",
#     "size": 1669,
#     "url": "https://api.github.com/repos/github/choosealicense.com/contents/_licenses/0bsd.txt?ref=gh-pages",
#     "html_url": "https://github.com/github/choosealicense.com/blob/gh-pages/_licenses/0bsd.txt",
#     "git_url": "https://api.github.com/repos/github/choosealicense.com/git/blobs/be73691957124783ddcf0353a0db98860dd3de62",
#     "download_url": "https://raw.githubusercontent.com/github/choosealicense.com/gh-pages/_licenses/0bsd.txt",
#     "type": "file",
#     "_links": {
#         "self": "https://api.github.com/repos/github/choosealicense.com/contents/_licenses/0bsd.txt?ref=gh-pages",
#         "git": "https://api.github.com/repos/github/choosealicense.com/git/blobs/be73691957124783ddcf0353a0db98860dd3de62",
#         "html": "https://github.com/github/choosealicense.com/blob/gh-pages/_licenses/0bsd.txt"
#     }
# }

# 通过 github REST api 获取仓库目录文件列表信息
github_repo_api ="https://api.github.com/repos/github/choosealicense.com/contents/_licenses"
resContent = requests.get(github_repo_api).content.decode('utf-8')

# proxy: 使用反向代理地址
rawgiturl = "https://raw.githubusercontent.com/github/choosealicense.com"
proxy = "https://github.progcz.workers.dev/github/choosealicense.com/raw"
# https://raw.githubusercontent.com/github/choosealicense.com/gh-pages/_licenses/0bsd.txt
# https://github.progcz.workers.dev/github/choosealicense.com/raw/gh-pages/_licenses/0bsd.txt

# 存储路径
prefix = "_licenses"
try:
    os.mkdir(prefix)
except:
    pass

remotes = json.loads(resContent)
for remote in remotes:
    name = remote["name"]
    download_url = str(remote["download_url"]).replace(rawgiturl, proxy)

    if not os.path.exists(os.path.join(prefix, name)):
        content = requests.get(download_url).content.decode('utf-8')
        with open(os.path.join(prefix, name), 'w') as f:
            f.write(content)
            f.flush()
    print(name)


# 准备所有文件并组成路径列表，
fileNames = os.listdir(prefix)
paths = [os.path.join(prefix, i) for i in fileNames if not "body" in i and not "head" in i]

_licenses = []


# 准备组织信息
# 准备拼接仓库前缀
repoPrefix = "https://gitee.com/zinface/youwant.license/raw/master/SPDX/_licenses"

for path in paths:
    with open(path, 'r') as file:
        name = os.path.basename(path)
        # os.path.basename(path)
        _file_head_name = ".".join(name.split('.')[0:-1]) + "_head.txt"
        _file_body_name = ".".join(name.split('.')[0:-1]) + "_body.txt"
        _license = {}
        _license["name"] = name
        _license["url"] = os.path.join(repoPrefix, _file_body_name)
        filecontent = "".join(file.readlines())
        file.close()

        header_end_index = filecontent.find('---', 15) + 3
        header = filecontent[:header_end_index].split("---")[1]
        header_strip = header.strip()
        
        body = filecontent[header_end_index+2:]
        # body_strip = body.strip()
        
        file = open(os.path.join(prefix, _file_head_name),'w')
        file.write(filecontent[0:header_end_index])
        file.flush()
        file.close()
        
        file = open(os.path.join(prefix, _file_body_name),'w')
        file.write(body)
        file.flush()
        file.close()

        os.remove(os.path.join(prefix, name))

        for part in header_strip.split("\n\n"):
            for line in part.split("\n"):
                if line.startswith("title"):
                    _license["title"] = line.strip()[len("title: "):]
                if line.startswith("spdx-id"):
                    _license["spdx-id"] = line.strip()[len("spdx-id: "):]
                if line.startswith("description"):
                    _license["description"] = line.strip()[len("description: "):]
                if line.startswith("how"):
                    _license["how"] = line.strip()[len("how: "):]
                if line.startswith("note"):
                    _license["note"] = line.strip()[len("note: "):]
            if part.startswith("permissions"):
                _license["permissions"] = []
                if "[]" in part:
                    continue
                for line in part.split("\n"):
                    if line.strip().startswith("-"):
                        _license["permissions"].append(line.strip().split(" ")[1])
            if part.startswith("conditions"):
                _license["conditions"] = []
                if "[]" in part:
                    continue
                for line in part.split("\n"):
                    if line.strip().startswith("-"):
                        _license["conditions"].append(line.strip().split(" ")[1])
            if part.startswith("limitations"):
                _license["limitations"] = []
                if "[]" in part:
                    continue
                for line in part.split("\n"):
                    if line.strip().startswith("-"):
                        _license["limitations"].append(line.strip().split(" ")[1])
        # print(_license)
        _licenses.append(_license)
# print(json.dumps(_licenses))

# 生成 licence.json
with open("licenses.json", "w") as jsonfile:
    jsonfile.write(json.dumps(_licenses))
    jsonfile.flush()